package work.timejay.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @className:WebMvcConfig.java
 * @author:王文杰
 * @version:1.0.0
 * @description:mvc配置类
 * @createTime 2023年12月07日 15:37:00
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /*
     自定义消息转换器 Long -> String
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        //fastJson
//        问题 需要加依赖
//        官网：https://baomidou.gitee.io/mybatis-plus-doc/#/question?id=id_worker-%e4%b8%bb%e9%94%ae-long-%e5%af%bc%e8%87%b4-js-%e7%b2%be%e5%ba%a6%e4%b8%a2%e5%a4%b1
//        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fjc = new FastJsonConfig();
//        //1、序列化重点
//        fjc.setSerializerFeatures(SerializerFeature.BrowserCompatible);
//        fastJsonConverter.setFastJsonConfig(fjc);
//        converters.add(fastJsonConverter);
    /**
     * 序列换成json时,将所有的long变成string
     * 因为js中得数字类型不能包含所有的java long值
     *  存在一个问题，就是 一般的api工具无法识别swagger 接口json数据
     */
//        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        objectMapper.registerModule(simpleModule);
//        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        converters.add(0,jackson2HttpMessageConverter);
//    }

    /**
     * 解决 ID_WORKER 生成主键太长导致 js 精度丢失
     * 官网：https://baomidou.gitee.io/mybatis-plus-doc/#/question?id=id_worker-%e4%b8%bb%e9%94%ae-long-%e5%af%bc%e8%87%b4-js-%e7%b2%be%e5%ba%a6%e4%b8%a2%e5%a4%b1
     *
     * @param builder
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        // 全局配置序列化返回 JSON 处理
        SimpleModule simpleModule = new SimpleModule();
        //JSON Long ==> String
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
